import { login, getInfo, getUserIdList } from "api/user";
import { Toast } from "vant";
import { getToken, setToken, removeToken } from "@/utils/auth";
import { resetRouter } from "@/router";
// import router from '@/router'

const LOGIN = "LOGIN"; // 获取用户信息
const SetUserData = "SetUserData"; // 获取用户信息
const LOGOUT = "LOGOUT"; // 退出登录、清除用户数据
const USER_DATA = "userDate"; // 用户数据

export default {
  namespaced: true,
  state: {
    token: getToken() || "",
    user: JSON.parse(localStorage.getItem(USER_DATA) || null)
  },
  mutations: {
    [LOGIN](state, data) {
      let userToken = data.data;
      state.token = userToken;
      setToken(userToken);
    },

    [SetUserData](state, userData = {}) {
      state.user = userData;
      localStorage.setItem(USER_DATA, JSON.stringify(userData));
    },
    [LOGOUT](state) {
      state.user = null;
      state.token = null;
      removeToken();
      localStorage.removeItem(USER_DATA);
      resetRouter();
    }
  },
  actions: {
    async login(state, data) {
      try {
        let res = await login({
          phoneNumber: data.phoneNumber,
          password: data.password
        });
        state.commit(LOGIN, res);
        Toast({
          message: "登录成功",
          position: "middle",
          duration: 1500
        });
        setTimeout(() => {
          const redirect = data.$route.query.redirect || "/";
          data.$router.replace({
            path: redirect
          });
        }, 1500);
      } catch (error) {}
    },
    // get user info
    getInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token)
          .then(response => {
            const { data } = response;

            if (!data) {
              // eslint-disable-next-line
              reject("Verification failed, please Login again.");
            }
            commit(SetUserData, data);
            resolve(data);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    getUserIdList({}, payload) {
      return new Promise((resolve, reject) => {
        if (process.env.VUE_APP_BUILDENV == "yzj") {
          getUserIdList({ ticket: payload })
            .then(res => {
              resolve(res);
            })
            .catch(err => {
              reject(err);
            });
        } else {
          let userCacheStr = sessionStorage.getItem("userCache");
          if (userCacheStr) {
            let userCache = JSON.parse(userCacheStr);
            resolve({
              code: "000000",
              success: true,
              data: userCache
            });
          } else {
            getUserIdList({ code: payload })
              .then(res => {
                if (res && res.success) {
                  let cache = {};
                  let flag = false;
                  if (res.data.roleType) {
                    cache.roleType = res.data.roleType;
                    flag = true;
                  }
                  if (res.data.userIdList) {
                    cache.userIdList = res.data.userIdList;
                    flag = true;
                  }
                  if (res.data.userName) {
                    cache.userName = res.data.userName;
                  }
                  if (flag) {
                    sessionStorage.setItem("userCache", JSON.stringify(cache));
                  }
                }
                resolve(res);
              })
              .catch(err => {
                reject(err);
              });
          }
        }
      });
    }
  },
  getters: {
    token(state) {
      return state.token;
    },
    user(state) {
      return state.user;
    }
  }
};
